package com.etooling.pdm.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.etooling.framework.jpa.JpaQueryBuilder;
import com.etooling.framework.jpa.Specifications;
import com.etooling.framework.svo.ReplyObj;
import com.etooling.framework.utils.WebUtils;
import com.etooling.pdm.entity.Goods;
import com.etooling.pdm.entity.GoodsSpecsPropSetting;
import com.etooling.pdm.repository.GoodsSpecsPropSettingRepository;
import com.etooling.pdm.service.GoodsSpecsPropSettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author dan
 * @date 2023/12/25 20:42
 */
@Service
public class GoodsSpecsPropSettingServiceImpl implements GoodsSpecsPropSettingService {
    @Autowired
    GoodsSpecsPropSettingRepository goodsSpecsPropSettingRepository;


    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public ReplyObj add(HttpServletRequest request, String json) {
        if (WebUtils.checkStr(json)) {
            try {
                JSONObject jsonObject = JSON.parseObject(json);
                String goodsId = jsonObject.getString("goodsId");
                if(WebUtils.checkStr(goodsId)){
                    GoodsSpecsPropSetting setting = new GoodsSpecsPropSetting();
                    String name = jsonObject.getString("name");
                    setting.setName(name);

                    Integer seq = jsonObject.getInteger("seq");
                    if (WebUtils.checkStr(seq)) {
                        setting.setSeq(seq);

                        Integer countSeq = goodsSpecsPropSettingRepository.countSeq(goodsId, seq);
                        if (countSeq > 0) {
                            // 把所有比这个seq大的都加1
                            JpaQueryBuilder<GoodsSpecsPropSetting> pb = Specifications.and();
                            pb.eq("goods.id", goodsId);
                            pb.ge("seq", seq - 1);
                            List<GoodsSpecsPropSetting> all = goodsSpecsPropSettingRepository.findAll(pb.build());
                            for (GoodsSpecsPropSetting propSetting : all) {
                                propSetting.setSeq(propSetting.getSeq() + 1);
                            }
                            goodsSpecsPropSettingRepository.saveAll(all);
                        }
                    } else {
                        Integer maxSeq = goodsSpecsPropSettingRepository.getMaxSeq(goodsId);
                        if (maxSeq == null) {
                            maxSeq = 0;
                        }
                        seq = maxSeq + 3;
                        setting.setSeq(seq);
                    }

                    setting.setStatus(GoodsSpecsPropSetting.STATUS_RUNNING);
                    String companyId = WebUtils.getSessionValue(request, WebUtils.LOGIN_COMPANY_ID_KEY);
                    setting.setCompanyId(companyId);
                    Goods goods = new Goods();
                    goods.setId(goodsId);
                    setting.setGoods(goods);
                    goodsSpecsPropSettingRepository.save(setting);
                    return WebUtils.flushSuccess();
                } else {
                    return WebUtils.flushFailure("没有选择产品，数据错误");
                }
            } catch (Exception e) {
                e.printStackTrace();
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return WebUtils.flushFailure("添加属性时发生数据解析异常");
            }
        } else {
            return WebUtils.flushFailure("添加属性的内容为空，无法添加");
        }
    }


    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public ReplyObj update(HttpServletRequest request, String json) {
        if (WebUtils.checkStr(json)) {
            try {
                JSONObject jsonObject = JSON.parseObject(json);

                String id = jsonObject.getString("id");
                if (WebUtils.checkStr(id)) {
                    GoodsSpecsPropSetting setting = goodsSpecsPropSettingRepository.getById(id);
                    String goodsId = setting.getGoods().getId();

                    String name = jsonObject.getString("name");
                    setting.setName(name);

                    Integer seq = jsonObject.getInteger("seq");
                    if (WebUtils.checkStr(seq)) {
                        setting.setSeq(seq);

                        Integer countSeq = goodsSpecsPropSettingRepository.countSeq(id, goodsId, seq);
                        if (countSeq > 0) {
                            // 把所有比这个seq大的都加1
                            JpaQueryBuilder<GoodsSpecsPropSetting> pb = Specifications.and();
                            pb.eq("goods.id", goodsId);
                            pb.ge("seq", seq - 1);
                            pb.ne("id", id);
                            List<GoodsSpecsPropSetting> all = goodsSpecsPropSettingRepository.findAll(pb.build());
                            for (GoodsSpecsPropSetting propSetting : all) {
                                propSetting.setSeq(propSetting.getSeq() + 1);
                            }
                            goodsSpecsPropSettingRepository.saveAll(all);
                        }
                    } else {
                        Integer maxSeq = goodsSpecsPropSettingRepository.getMaxSeq(goodsId);
                        if (maxSeq == null) {
                            maxSeq = 0;
                        }
                        seq = maxSeq + 3;
                        setting.setSeq(seq);
                    }

                    goodsSpecsPropSettingRepository.save(setting);
                    return WebUtils.flushSuccess();
                } else {
                    return WebUtils.flushFailure("找不到对应属性，无法修改");
                }
            } catch (Exception e) {
                e.printStackTrace();
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return WebUtils.flushFailure("修改属性时发生数据解析异常");
            }
        } else {
            return WebUtils.flushFailure("修改属性的内容为空，无法添加");
        }
    }


}
